#include <stdio.h>

int match_star( char c, char* regex, char* text );

int match( char* regex, char* text )
{
  if( regex[0] == '^' )
    return match_here( regex+1, text );
  do {
    if( match_here( regex, text ) )
      return 1;
  } while( *text++ != '\0' );

  return 0;
}

int match_here( char* regex, char* text )
{
  if( regex[0] == '\0' )
    return 1;
  
  if( regex[0] == '$' )
    return text[0]=='\0';

  if( regex[1] == '*' )
    return match_star( regex[0], regex+2, text );
  
  if( text[0] != '\0' && ( regex[0] == '?' || regex[0] == text[0] ) )
    return match_here( regex+1, text+1 );
  
  return 0;
}

int match_star( char c, char* regex, char* text )
{
  do {
    if( match_here( regex, text ) )
      return 1;
  } while( *text != '\0' && ( *text++ == c || c == '?' ) );

  return 0;
}


int main()
{
  char* str = "hello";
  char* reg = "^hel*oo";
  
  printf("%d\n", match(reg, str));
  
  return 0;
}

